package com.java.business.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.java.business.dao.MaterialMapper;
import com.java.business.dao.PickingMapper;
import com.java.business.model.Material;
import com.java.business.model.Picking;
import com.java.business.service.MaterialService;
import com.java.business.service.PickingService;
import com.java.common.model.Result;
import com.java.manage.model.Accessories;
import com.java.manage.service.api.AccessoriesApiService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class PickingImpl implements PickingService {

    @Autowired
    private PickingMapper pickingMapper;

    /**
     * 关联配件表
     */
    @DubboReference
    private AccessoriesApiService accessoriesApiService;


    @Transactional
    @Override
    public Result save(Picking picking) {
        picking.setCreateTime(new Date());
        picking.setUseTime(new Date());
        pickingMapper.save(picking);

        return Result.success();
    }

    @Transactional
    @Override
    public Result modify(Picking picking) {
        pickingMapper.modify(picking);

        return Result.success();
    }

    @Transactional
    @Override
    public Result remove(int[] ids) {
        for (int id : ids) {
            pickingMapper.remove(id);
        }

        return Result.success();
    }

    @Override
    public Result list(Picking picking) {
        PageHelper.startPage(picking.getPageNum(), picking.getPageSize());
        List<Picking> list = pickingMapper.list(picking);

        for (Picking materials : list) {
            // 配件表
            Accessories accessories = accessoriesApiService.listAll(materials.getAccessoriesId());
            materials.setAccessories(accessories);
        }

        PageInfo<Picking> pageInfo = new PageInfo<>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("list", pageInfo.getList());
        map.put("total", pageInfo.getTotal());

        return Result.success(map);
    }

    @Override
    public Result get(int id) {
        Picking book = pickingMapper.get(id);

        return Result.success(book);
    }




}
